from google.appengine.ext import db
from horseanalysis.lib.callable import Callable
from horseanalysis.model.race import Race
from horseanalysis.model.race_batch import RaceBatch

class WinPlace(db.Model):
  race = db.ReferenceProperty(Race)
  batch_time = db.ReferenceProperty(RaceBatch)
  horse_no = db.IntegerProperty()
  win = db.FloatProperty()
  place = db.FloatProperty()
  
  def create_new_entry(race, batch_time, horse_no, win, place):
    entry = WinPlace(race=race, batch_time=batch_time, horse_no=horse_no, win=win, place=place)
    entry.put()
  
  def get_odds(batch_time):
    return WinPlace.gql('WHERE batch_time = :1 ORDER BY horse_no', batch_time)
  
  def get_win_odds(batch_time):
    result = {}
    odds_collection = WinPlace.get_odds(batch_time)
    for odds in odds_collection:
      result[odds.horse_no] = odds.win
    return result
  
  def get_place_odds(batch_time):
    result = {}
    odds_collection = WinPlace.get_odds(batch_time)
    for odds in odds_collection:
      result[odds.horse_no] = odds.place
    return result
    
  create_new_entry = Callable(create_new_entry)
  get_odds = Callable(get_odds)
  get_win_odds = Callable(get_win_odds)
  get_place_odds = Callable(get_place_odds)